package com.harmonyos.samples.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.intellij.ui.jcef.JBCefBrowser;
import org.cef.browser.CefBrowser;
import org.cef.browser.CefFrame;
import org.cef.handler.CefLoadHandlerAdapter;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class StatisticsUtils {
    public static void statistics() {
        String url = "https://hm.baidu.com/hm -collect";
        HttpURLConnection httpURLConnection = null;
        OutputStream ops = null;
        try {
            httpURLConnection = (HttpURLConnection) (new URL(url).openConnection());
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setRequestProperty("Content-Type", "application/json");
            httpURLConnection.setRequestProperty("Accept", "application/json");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            httpURLConnection.connect();
            ops = httpURLConnection.getOutputStream();
            Map<String, String> map = new HashMap<>();
            map.put("siteid", "fa40bbef89c199b6d3d01196a3b136de");
            map.put("eid", "打开插件");
            ops.write(new ObjectMapper().writeValueAsString(map).getBytes());
            ops.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (httpURLConnection != null) {
                httpURLConnection.disconnect();
            }
            if (ops != null) {
                try {
                    ops.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    public static void loadBaiduTongjiScript(JBCefBrowser browser) {
        String javascriptEventTrackingCode = "javascript:_hmt.push(['_trackEvent', 'Plugin', 'Button_Click', 'MyButton']);";
        try {
            InputStream inputStream = StatisticsUtils.class.getResourceAsStream("/baidu_tongji.js");
            if (inputStream != null) {
                byte[] buffer = inputStream.readAllBytes();
                String scriptContent = new String(buffer, StandardCharsets.UTF_8);
                browser.getJBCefClient().addLoadHandler(new CefLoadHandlerAdapter() {
                    @Override
                    public void onLoadEnd(CefBrowser browser, CefFrame frame, int httpStatusCode) {
                        super.onLoadEnd(browser, frame, httpStatusCode);
                        browser.executeJavaScript(scriptContent, browser.getURL(), 0);
//                        browser.executeJavaScript(javascriptEventTrackingCode, "", 0);
                    }
                }, browser.getCefBrowser());
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
